package com.aos.iot.protocol.hll.client;

import cn.hutool.core.util.StrUtil;
import com.aos.iot.protocol.hll.message.HllMessage;
import com.aos.iot.socket.common.client.IClientBox;
import io.netty.channel.Channel;
import io.netty.util.internal.PlatformDependent;
import org.springframework.stereotype.Component;

import java.net.SocketAddress;
import java.util.Map;

import static com.aos.iot.protocol.hll.constant.HllConstant.PROTOCOL_NAME;

@Component
public class HllClientBox implements IClientBox<HllMessage, HllClient> {

    private final Map<String, HllClient> clientId2clients = PlatformDependent.newConcurrentHashMap();

    private final Map<String, SocketAddress> clientId2RemoteAddress = PlatformDependent.newConcurrentHashMap();

    private final Map<Channel, String> channel2clientId = PlatformDependent.newConcurrentHashMap();

    private final Map<Channel, SocketAddress> channel2RemoteAddress = PlatformDependent.newConcurrentHashMap();

    @Override
    public void add(String clientId, HllClient client, SocketAddress socketAddress) {
        remove(clientId);
        clientId2clients.put(clientId, client);
        channel2clientId.put(client.getChannel(), clientId);
        clientId2RemoteAddress.put(clientId, socketAddress);
        channel2RemoteAddress.put(client.getChannel(), socketAddress);
        client.setClientId(clientId);
    }

    @Override
    public HllClient get(String clientId) {
        return clientId2clients.get(clientId);
    }

    @Override
    public HllClient get(Channel channel) {
        String clientId = channel2clientId.get(channel);
        if(StrUtil.isNotBlank(clientId)) {
            return clientId2clients.get(clientId);
        }
        return null;
    }

    @Override
    public SocketAddress getRemoteAddress(String clientId) {
        return clientId2RemoteAddress.get(clientId);
    }

    @Override
    public SocketAddress getRemoteAddress(Channel channel) {
        return channel2RemoteAddress.get(channel);
    }

    @Override
    public void updateRemoteAddress(HllClient client, SocketAddress socketAddress) {
        clientId2RemoteAddress.put(client.getClientId(), socketAddress);
        channel2RemoteAddress.put(client.getChannel(), socketAddress);
    }

    @Override
    public void remove(String clientId) {
        if(StrUtil.isNotBlank(clientId)) {
            HllClient client = clientId2clients.remove(clientId);
            if (client != null) {
                channel2clientId.remove(client.getChannel());
                clientId2RemoteAddress.remove(client.getClientId());
                channel2RemoteAddress.remove(client.getChannel());
                client.setClientId(null);
            }
        }
    }

    @Override
    public void remove(Channel channel) {
        if(channel != null) {
            String clientId = channel2clientId.remove(channel);
            if (StrUtil.isNotBlank(clientId)) {
                HllClient client = clientId2clients.remove(clientId);
                if (client != null) {
                    clientId2RemoteAddress.remove(client.getClientId());
                    channel2RemoteAddress.remove(client.getChannel());
                    client.setClientId(null);
                }
            }
        }
    }

    @Override
    public boolean has(String clientId) {
        return clientId2clients.containsKey(clientId);
    }

    @Override
    public boolean has(Channel channel) {
        return channel2clientId.containsKey(channel);
    }

    @Override
    public int size() {
        return channel2clientId.size();
    }

    @Override
    public String protocolName() {
        return PROTOCOL_NAME;
    }
}
